var express = require('express');
var router = express.Router();
var customer = require('../data/customer');
var fs = require('fs');

router.get('/listhot', function (req, res, next) {
  var page = req.query.page;
  var ListHot = [];
  for (var i = 0; i < 10; i++) {
    ListHot.push({
      name: 'listhot' + (page * 100 + i),
      id: page
    })
  }
  res.send(ListHot);
});

router.get('/listnew', function (req, res, next) {
  var page = req.query.page;
  var ListHot = [{
    name: 'listnew' + page * 100,
    id: page
  }, {
    name: 'listnew' + page * 100 + 1,
    id: page + 1
  }
  ];
  res.send(ListHot);
});

router.use('/dianji', function (req, res, next) {
  console.log(req.headers.cookie);
  res.send('123');
});

router.use('/GetOrPostDetails', function (req, res, next) {

  function sendCustomer() {
    res.send(customer);
  }

  console.log(req.body, req.query);
  // res.send(400, '');
  setTimeout(sendCustomer, 5000);

});

router.use('/TestLoading', function (req, res, next) {

  function sendCustomer() {
    res.send([]);
  }

  console.log(req.body, req.query);
  // res.send(400, '');
  setTimeout(sendCustomer, 10000);

});

router.delete('/delete', function (req, res, next) {
  console.log(req.body, req.query, req.params);
  res.send('delete')
})


router.use('/assets', function (req, res, next) {
  console.log(req.body, req.query);
  res.send({msg: 'GetOrPostDetails', arr: [1, 2, 3, 4], obj: {a: 1, b: 2}});
});

router.use('/error', function (req, res, next) {
  res.send(400);
});

// 工艺文件预览
router.get('/dl', function (req, res, next) {
  console.log(1)
  var path="C:\\Users\\liubh\\Desktop\\CAPPView\\测试工艺.cxp";
  var f = fs.createReadStream(path);
  res.writeHead(200, {
    'Content-Type': 'application/force-download',
    'Content-Disposition': 'attachment; filename=test.cxp'
  });
  f.pipe(res);
})

// 工艺文件预览
router.get('/pdf', function (req, res, next) {
  var path="C:\\Users\\liubh\\Desktop\\test.pdf";
  var f = fs.createReadStream(path);
  res.writeHead(200, {
    'Content-Type': 'application/force-download',
    'Content-Disposition': 'attachment; filename=test.pdf'
  });
  f.pipe(res);
})

module.exports = router;
